package com.jier.controller.console;

import com.jier.annotation.ValidateField;
import com.jier.annotation.ValidateGroup;
import com.jier.common.Result;
import com.jier.common.exception.HttpParamException;
import com.jier.common.permission.Permission;
import com.jier.controller.BaseController;
import com.jier.dto.SystemPermissionResultDto;
import com.jier.entities.SystemPermission;
import com.jier.model.PageParam;
import com.jier.model.SystemPermissionParam;
import com.jier.service.ISystemPermissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * 系统权限控制类
 *
 * @author jie_r
 * @date 2018/03/18
 */
@RestController
@RequestMapping("/console/systemPermissions")
public class SystemPermissionController extends BaseController {
    @Autowired
    private ISystemPermissionService systemPermissionService;

    /**
     * 系统权限首页-管理页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/index.html", method = RequestMethod.GET)
    @ApiOperation(value = "系统权限首页-管理页面", hidden = true, httpMethod = "GET")
    @Permission(code = "010401", content = "系统权限首页-管理页面")
    public ModelAndView indexPage() throws Exception {
        return new ModelAndView("/systemPermission/systemPermissions");
    }

    /**
     * 编辑系统权限-管理页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/edit.html", method = RequestMethod.GET)
    @ApiOperation(value = "编辑系统权限-管理页面", hidden = true, httpMethod = "GET")
    @Permission(code = "010403", content = "编辑系统权限-管理页面")
    public ModelAndView editPage(Integer id) throws Exception {
        ModelAndView mv = new ModelAndView("/systemPermission/edit_systemPermission");
        SystemPermission systemPermission = systemPermissionService.getById(id);
        mv.addObject("systemPermission", systemPermission);
        return mv;
    }

    /**
     * 编辑系统权限-管理页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/add.html", method = RequestMethod.GET)
    @ApiOperation(value = "添加系统权限-管理页面", hidden = true, httpMethod = "GET")
    @Permission(code = "010402", content = "添加系统权限-管理页面")
    public ModelAndView addPage() throws Exception {
        return new ModelAndView("/systemPermission/add_systemPermission");
    }

    /**
     * 分页获取系统权限
     *
     * @param moduleId
     * @param pageParam
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json;")
    @ApiOperation(value = "分页获取系统权限", httpMethod = "GET", produces = "application/json;")
    @Permission(code = "010401", content = "分页获取系统权限")
    public ResponseEntity<Result<SystemPermissionResultDto>> getSystemPermissions(Integer moduleId, PageParam pageParam) throws Exception {
        Result<SystemPermissionResultDto> result = systemPermissionService.getSystemPermissions(moduleId, getPage(pageParam));
        return ok(result);
    }

    /**
     * 编辑系统权限
     *
     * @param permissionParam
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.POST, produces = "application/json;")
    @ApiOperation(value = "编辑系统权限", httpMethod = "POST", produces = "application/json;")
    @ValidateGroup(fields = {
            @ValidateField(index = 0, paramName = "名字", filedName = "permissionParam.name", notNull = true, maxLen = 10),
            @ValidateField(index = 0, paramName = "编号", filedName = "permissionParam.code", notNull = true, regStr = "^\\d{6}$"),
            @ValidateField(index = 0, paramName = "模块ID", filedName = "permissionParam.moduleId", notNull = true),
            @ValidateField(index = 0, paramName = "状态", filedName = "permissionParam.status", notNull = true),
    })
    @Permission(code = "010403", content = "编辑系统权限")
    public ResponseEntity<Boolean> updateSystemModule(SystemPermissionParam permissionParam, @PathVariable Integer id) throws Exception {
        SystemPermission systemPermission = systemPermissionService.getById(id);
        if (systemPermission == null) {
            throw new HttpParamException("系统权限信息不存在");
        }
        SystemPermission systemPermissionTmp = systemPermissionService.getSystemPermissionByNumber(permissionParam.getCode());
        if (systemPermissionTmp != null && !systemPermissionTmp.getId().equals(systemPermission.getId())) {
            throw new HttpParamException("系统权限，编号【" + permissionParam.getCode() + "】已存在");
        }
        systemPermissionTmp = systemPermissionService.getSystemModuleByName(permissionParam.getName(), permissionParam.getModuleId());
        if (systemPermissionTmp != null && !systemPermissionTmp.getId().equals(systemPermission.getId())) {
            throw new HttpParamException("系统权限，同一模块下名字【" + permissionParam.getName() + "】已存在");
        }
        systemPermission.setName(permissionParam.getName());
        systemPermission.setCode(permissionParam.getCode());
        systemPermission.setModuleId(permissionParam.getModuleId());
        systemPermission.setStatus(permissionParam.getStatus());
        systemPermissionService.updateById(systemPermission);
        return ok(Boolean.TRUE);
    }

    /**
     * 添加系统权限
     *
     * @param permissionParam
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json;")
    @ApiOperation(value = "添加系统权限", httpMethod = "POST", produces = "application/json;")
    @ValidateGroup(fields = {
            @ValidateField(index = 0, paramName = "名字", filedName = "permissionParam.name", notNull = true, maxLen = 10),
            @ValidateField(index = 0, paramName = "编号", filedName = "permissionParam.code", notNull = true, regStr = "^\\d{6}$"),
            @ValidateField(index = 0, paramName = "模块ID", filedName = "permissionParam.moduleId", notNull = true),
            @ValidateField(index = 0, paramName = "状态", filedName = "permissionParam.status", notNull = true),
    })
    @Permission(code = "010402", content = "添加系统权限")
    public ResponseEntity<Boolean> addSystemModule(SystemPermissionParam permissionParam) throws Exception {
        SystemPermission systemPermissionTmp = systemPermissionService.getSystemPermissionByNumber(permissionParam.getCode());
        if (systemPermissionTmp != null) {
            throw new HttpParamException("系统权限，编号【" + permissionParam.getCode() + "】已存在");
        }
        systemPermissionTmp = systemPermissionService.getSystemModuleByName(permissionParam.getName(), permissionParam.getModuleId());
        if (systemPermissionTmp != null) {
            throw new HttpParamException("系统权限，同一模块下名字【" + permissionParam.getName() + "】已存在");
        }
        SystemPermission systemPermission = new SystemPermission();
        systemPermission.setName(permissionParam.getName());
        systemPermission.setCode(permissionParam.getCode());
        systemPermission.setModuleId(permissionParam.getModuleId());
        systemPermissionTmp = systemPermissionService.getSystemPermissionLast(permissionParam.getModuleId());
        if (systemPermissionTmp == null) {
            systemPermission.setSortOrder(1);
        } else {
            systemPermission.setSortOrder(systemPermissionTmp.getSortOrder() + 1);
        }
        systemPermission.setStatus(permissionParam.getStatus());
        systemPermission.setUpdateDate(new Date());
        systemPermission.setCreateDate(new Date());
        systemPermissionService.save(systemPermission);
        return ok(Boolean.TRUE);
    }
}
